#!/usr/bin/python
# encoding=utf-8
# cosine 2010-05

"""HTML链接解析模块"""

import re
from sgmllib import SGMLParser

class LinkParse(SGMLParser):
	"""html链接解析"""

	def __init__(self,tags):
		"""初始化获取对象，目标得到的urls为dict结构"""
		self.urls = {}
		
		self._a = self._area = self._iframe = self._frame = self._meta = self._script = self._form = self._img = self._style = 0
		if not tags:
			self._a = 1
			self._area = 1
			self._iframe = 1
			self._frame = 1
			self.urls['a'] = []
			self.urls['area'] = []
			self.urls['iframe'] = []
			self.urls['frame'] = []
		else:
			for tag in tags:
				if tag == 'a':
					self._a = 1
					self.urls['a'] = []
				if tag == 'area':
					self._area = 1
					self.urls['area'] = []
				if tag == 'iframe':
					self._iframe = 1
					self.urls['iframe'] = []
				if tag == 'frame':
					self._frame = 1
					self.urls['frame'] = []
				if tag == 'meta':
					self._meta = 1
					self.urls['meta'] = []
				if tag == 'script':
					self._script = 1
					self.urls['script'] = []
				if tag == 'form':
					self._form = 1
					self.urls['form'] = []
				if tag == 'img':
					self._img = 1
					self.urls['img'] = []
				if tag == 'style':
					self._style = 1
					self.urls['style'] = []

		self.reset()

	def reset(self):
		SGMLParser.reset(self)

	def start_a(self, attrs):
		if self._a == 1:
			href = [v for k, v in attrs if k=='href']
			if href:
				self.urls['a'].extend(href)
	def start_area(self, attrs):
		if self._area == 1:
			href = [v for k, v in attrs if k=='href']
			if href:
				self.urls['area'].extend(href)
	def start_iframe(self, attrs):
		if self._iframe == 1:
			src = [v for k, v in attrs if k=='src']
			if src:
				self.urls['iframe'].extend(src)
	def start_frame(self, attrs):
		if self._frame == 1:
			src = [v for k, v in attrs if k=='src']
			if src:
				self.urls['frame'].extend(src)
	def start_meta(self, attrs):
		if self._meta == 1:
			try:
				http_equiv = [v for k, v in attrs if k=='http-equiv']
				if str(http_equiv).lower() == "['refresh']":
					metaurl = [v for k, v in attrs if k=='content']
					metaurl = ''.join(metaurl)
					c = re.compile(r'''\s*\d\s*;\s*url\s*=\s*['\"]?\s*([^><'\" ]*)\s*['\"]?''',re.I)
					url = c.findall(metaurl)
					self.urls['meta'].extend(url)
			except:
				pass
	def start_script(self, attrs):
		if self._script == 1:
			src = [v for k, v in attrs if k=='src']
			if src:
				self.urls['script'].extend(src)
	def start_form(self, attrs):
		if self._form == 1:
			action = [v for k, v in attrs if k=='action']
			if action:
				self.urls['form'].extend(action)
	def start_img(self, attrs):
		if self._img == 1:
			src = [v for k, v in attrs if k=='src']
			if src:
				self.urls['img'].extend(src)
	def start_link(self, attrs):
		if self._style == 1:
			css_flag = False
			for k,v in attrs:
				if v == 'text/css':
					css_flag = True
					break
			if not css_flag:
				return
			src = [v for k, v in attrs if k=='href']
			if src:
				self.urls['style'].extend(src)

